package com.nf.health.app.datamain.activity;

import android.os.Bundle;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.BloodPressureHistoryDataAdapter;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.http.RequestProvider;
import com.nf.health.app.models.BloodPressureHistoryBean;
import com.nf.health.app.utils.ViewUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hepan on 2016/3/22.
 * The only genius that is worth anything is the genius for hard work
 */
public class BloodPressureHistoryDataActivity extends BaseActivity {

    @Bind(R.id.history_listview)
    DragListView historyListview;

    private RequestProvider provider;
    private static final String REQUEST_HISTORY_DATA = "REQUEST_HISTORY_DATA";
    private static final String REQUEST_MORE_DATA = "request_more_data";
    private static final String DELETE_DATA = "delete_data";
    private String pageNo = "1";
    private String pageSize = "10";
    private int totalPage = 1;

    //记录删除条目的位置
    private int deletePos = 0;

    //适配器
    private BloodPressureHistoryDataAdapter adapter;
    //请求的历史数据对象
    private BloodPressureHistoryBean extra;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.layout_common_header);
        setContent(R.layout.activity_blood_pressure_history_data);
        ButterKnife.bind(this);

        initView();
        requestData(pageNo, pageSize, REQUEST_HISTORY_DATA);
    }

    /**
     * 请求血压数据
     *
     * @param pageNo 页码
     * @param pageSize 数量
     * @param httpFlag 标记
     */
    private void requestData(String pageNo, String pageSize, String httpFlag) {
        if (provider == null) {
            provider = new RequestProvider(this);
        }
        if (REQUEST_HISTORY_DATA.equals(httpFlag)) {
            //首次请求数据
            provider.requestBloodHistoryData(pageNo,pageSize,httpFlag);
        }
        if (REQUEST_MORE_DATA.equals(httpFlag)) {
            //首次更多数据
            provider.requestBloodHistoryData(pageNo,pageSize,httpFlag);
        }
    }

    private void initView() {
        ViewUtil.setText(this, R.id.titlebar_title_tv, "历史记录");
        historyListview.setRefreshableAndLoadMoreable(true, true);
        historyListview.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {
                //加载下一页数据
                totalPage++;
                requestData(totalPage+"",pageSize,REQUEST_MORE_DATA);
            }

            @Override
            public void onLoadMore() {
                //加载下一页数据
                totalPage++;
                requestData(totalPage+"",pageSize,REQUEST_MORE_DATA);
            }
        });
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if (REQUEST_HISTORY_DATA.equals(actionName)){
            extra = (BloodPressureHistoryBean) object;
            if (adapter == null){
                adapter = new BloodPressureHistoryDataAdapter(this,extra);
                adapter.setOnMyClickListener(new OnMyClickListener() {
                    //点击删除对应id的历史数据
                    @Override
                    public void onClick(int position, String flag) {
                        deletePos = position;
                        int id = (int) extra.data.deviceData.get(position).id;
                        provider.requestDeleteHistory("blood", id + "", DELETE_DATA);
                    }
                });
            }else{
                adapter.setExtraData(extra);
            }
            historyListview.setAdapter(adapter);
        }else if (DELETE_DATA.equals(actionName)){
            showToast("删除成功");
            extra.data.deviceData.remove(deletePos);
            adapter.notifyDataSetChanged();//更新数据
        }else if(REQUEST_MORE_DATA.equals(actionName)){
            historyListview.completeRefresh();
            historyListview.completeLoadMore();
            BloodPressureHistoryBean extra1 = (BloodPressureHistoryBean) object;
            if(extra1 == null || extra1.data.deviceData.toString().equals("[]")||extra1.data.deviceData.size()<1){
                showToast("没有更多数据");
            }else{
                extra.data.deviceData.addAll(extra1.data.deviceData);
                adapter.notifyDataSetChanged();
            }
        }
    }
}
